/*
	使用Promise 封装uni.reqest
*/
import {
	API_BASE_URL
} from '../config/index.js'
export const Http = (url, data = {}, method = 'get') => {
	url = API_BASE_URL + url
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title:'加载中...'
		})
		uni.request({
			url,
			data,
			method,
			header: {
				'Authorization': uni.getStorageSync('token')
			},
			success: (res) => {
				// this.text='request success'
				if (res.data.errCode == '10000') {
					console.log(res.data);
					console.log(111);
					resolve(res.data)
				} else if(res.data.errCode == '10200'){
					uni.navigateTo({
						url:'/pages/login/login'
					})
					reject(res.data.errMsg)
				}else {
					uni.showToast({
						icon: 'error',
						title: res.data.errMsg,
					})
					reject(res.data.errMsg)
				}
			},
			fail: (error) => {
				if(error.errMsg==='request:fail'){
					uni.showToast({
						icon: 'error',
						title: '网络错误',
						duration:2000
					})
				}
				reject(error)
			},
			complete: () => {
				uni.hideLoading()
				console.log('complete');
			}
		})
	})
}

export const get = (url, data = {}) => {
	return Http(url, data, 'get')
}
export const post = (url, data = {}) => {
	return Http(url, data, 'post')
}